package com.wg.io.config;

import com.wg.io.pojo.Configuration;
import com.wg.io.pojo.MappedStatement;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;

/**
 * @author Mrwg
 * @date 2020/3/26 12:32
 * @description
 */
public class XMLMapperBuilder {

    private Configuration configuration;

    public XMLMapperBuilder(Configuration configuration) {
        this.configuration = configuration;
    }

    /**
     *
     * @param resourceSteam
     * @throws DocumentException
     *     <select id="selectOne" paramterType="com.lagou.pojo.User" resultType="com.lagou.pojo.User">
     *         select * from user where id = #{id} and username =#{username}
     *     </select>
     */
    public void parse(InputStream resourceSteam) throws DocumentException, ClassNotFoundException {
        Document document = new SAXReader().read(resourceSteam);
        Element rootElement = document.getRootElement();
        String namespace = rootElement.attributeValue("namesapce");
        List<Element> select = rootElement.selectNodes("select");
        for (Element element : select) {
            String id = element.attributeValue("id");
            String paramterType = element.attributeValue("parameterType");
            String resultType = element.attributeValue("resultType");
            //sql
            String textTrim = element.getTextTrim();
            //输入的参数Class
//            Class<?> paramterTypeclass = getClassType(paramterType);
            //返回参数
//            Class<?> resultTypeclass = getClassType(resultType);
           //statementId
            String key = namespace +"."+id;
           //分装MappedStatement
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setParamterType(paramterType);
            mappedStatement.setResultType(resultType);
            mappedStatement.setSql(textTrim);

            //填充configuration
            configuration.getMappedStatementMap().put(key,mappedStatement);

        }

        List<Element> insert = rootElement.selectNodes("insert");
        for (Element element : insert) {
            String id = element.attributeValue("id");
            String paramterType = element.attributeValue("parameterType");
            //sql
            String textTrim = element.getTextTrim();
            String key = namespace +"."+id;
            //分装MappedStatement
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setParamterType(paramterType);
            mappedStatement.setSql(textTrim);

            //填充configuration
            configuration.getMappedStatementMap().put(key,mappedStatement);
        }

        List<Element> update = rootElement.selectNodes("update");
        for (Element element : update) {
            String id = element.attributeValue("id");
            String paramterType = element.attributeValue("parameterType");
            //sql
            String textTrim = element.getTextTrim();
            String key = namespace +"."+id;
            //分装MappedStatement
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setParamterType(paramterType);
            mappedStatement.setSql(textTrim);

            //填充configuration
            configuration.getMappedStatementMap().put(key,mappedStatement);
        }

        List<Element> delete = rootElement.selectNodes("delete");
        for (Element element : delete) {
            String id = element.attributeValue("id");
            String paramterType = element.attributeValue("parameterType");
            //sql
            String textTrim = element.getTextTrim();
            String key = namespace +"."+id;
            //分装MappedStatement
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setParamterType(paramterType);
            mappedStatement.setSql(textTrim);

            //填充configuration
            configuration.getMappedStatementMap().put(key,mappedStatement);
        }
    }

    private Class<?> getClassType(String paramterType) throws ClassNotFoundException {
        Class<?> aClass = Class.forName(paramterType);
        return aClass;
    }
}
